#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include <iostream>
#include <typeinfo>

int main(void) {
    /* Initialize the library */
    if (!glfwInit()) { 
        return -1; 
    }
    
    /* Create a windowed mode window and its OpenGL context */
    GLFWwindow* window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window) {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Initialize glew for cross-platform opengl */
    if(GLenum state = glewInit(); state != GLEW_OK) { 
        std::cerr << "Error: glewInit() returned " << state << std::endl;
        return state; 
    }
    std::cout << "OpenGL Version:" << glGetString(GL_VERSION) << std::endl;

    uint buffer;
    glGenBuffers(1, &buffer); // Create a buffer on vram
    glBindBuffer(GL_ARRAY_BUFFER, buffer); // Select the buffer

    float vertices[6] = { // Define the positions of triangle vertices
        -0.5f, -0.5f,
         0.0f,  0.5f,
         0.5f, -0.5f
    };
    glBufferData( // Add data to current buffer
        GL_ARRAY_BUFFER, // Data "type" 
        6*sizeof(float), // Size of data
        vertices, // Data
        GL_STATIC_DRAW // Mode
    );
    glEnableVertexAttribArray(0); // Enable vertex attribute array support
    glVertexAttribPointer( // This function tells opengl how to interpret its current data
        0, // Starting byte
        2, // # values in each vertices
        GL_FLOAT,
        GL_FALSE,
        sizeof(float)*2, // Byte offset of each veritce
        0 // Byte offset of each attribute
    );

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window)) {
        /* Clear window each frame */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}
